package com.fromfuture.xuhuidoc.android.activity;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fromfuture.xuhuidoc.R;
import com.fromfuture.xuhuidoc.android.base.MyApplication;
import com.fromfuture.xuhuidoc.android.commonresouce.MyHtml;
import com.fromfuture.xuhuidoc.android.fragments.MyConsultRoomFragment01;
import com.fromfuture.xuhuidoc.android.fragments.MyConsultRoomFragment02;
import com.fromfuture.xuhuidoc.android.utils.MySharedPreferencesUtils;
import com.fromfuture.xuhuidoc.cordova.untils.CordovaAppManager;

import org.apache.cordova.CordovaActivity;

import java.util.ArrayList;
import java.util.List;

import app.auto.runner.base.utility.ToastUtil;

public class MyConsultRoomActivity extends CordovaActivity  implements MyConsultRoomFragment02.Doc_open_video{
    public MyConsultRoomFragment02 getMyConsultRoomFragment02() {
        return myConsultRoomFragment02;
    }

    private static List<Fragment> fragmentList;
    private MyConsultRoomFragment01 myConsultRoomFragment01;
    private MyConsultRoomFragment02 myConsultRoomFragment02;
    private static FragmentManager fragmentManager;
    private static ImageView button_back;
    private static TextView textView_top_name;

    private static int back_state=0;//0表示当前在第一个碎片，1表示在第二个碎片

    private  Fra01ItemStateBtn fra01ItemStateBtn;

    public  void setFra01ItemStateBtn(Fra01ItemStateBtn fra01ItemStateBtn) {
        this.fra01ItemStateBtn = fra01ItemStateBtn;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ToastUtil.get(this).toast("come");
//        loadUrl(MyHtml.DOCLOGIN);
        String guid = MySharedPreferencesUtils.getValue(this,"DOCINFO","DOCGUID","");
        String pwd = MySharedPreferencesUtils.getValue(this,"DOCINFO","DOCPWD","");
        loadUrl(MyHtml.MYCONSULTROOM01+"&DOCGUID="+guid+"&DOCPWD="+pwd);
        CordovaAppManager.myConsultRoomActivity=this;
        CordovaAppManager.mycordovaWebView=appView;
        setContentView(R.layout.activity_my_consult_room);
        findViewById();
        MyConsultRoom_init();
        callbackreceive();
        callback();
        findViewById(R.id.my_consult_room_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                out_my_consult_room_back();
            }
        });

    }

    private void MyConsultRoom_init(){
        myConsultRoomFragment01=new MyConsultRoomFragment01();
        myConsultRoomFragment02=new MyConsultRoomFragment02();

        fragmentList=new ArrayList<Fragment>();
        fragmentList.add(myConsultRoomFragment01);
        fragmentList.add(myConsultRoomFragment02);

        fragmentManager=getFragmentManager();
        FragmentTransaction transaction=fragmentManager.beginTransaction();
        transaction.add(R.id.my_consult_room_fragment,fragmentList.get(0),"myConsultRoomFragment01");
        transaction.add(R.id.my_consult_room_fragment,fragmentList.get(1),"myConsultRoomFragment02");
        transaction.show(fragmentList.get(0));
        transaction.hide(fragmentList.get(1));
        transaction.commit();
    }



    private void findViewById(){
        button_back= (ImageView) findViewById(R.id.my_consult_room_back);
        textView_top_name= (TextView) findViewById(R.id.my_consult_room_top_name);
    }

    protected void out_my_consult_room_back(){
        if (back_state==0){
            super.onBackPressed();
        }else if (back_state==1){
            textView_top_name.setText("我的病人");
            FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
            fragmentTransaction.show(fragmentList.get(0));
            fragmentTransaction.hide(fragmentList.get(1));
            back_state=0;
            fragmentTransaction.commit();
        }
    }

    @Override
    public void onBackPressed() {
        if (back_state==0){
            super.onBackPressed();
        }else if (back_state==1){
            FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
            fragmentTransaction.show(fragmentList.get(0));
            fragmentTransaction.hide(fragmentList.get(1));
            back_state=0;
            fragmentTransaction.commit();
        }
    }

    public static void onItemClickDoing(int pos, int state){
        switch (state){
            case 0:
                Toast.makeText(MyApplication.getAppContext(),"没反应"+pos,Toast.LENGTH_SHORT).show();
                break;
            case 1:
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                fragmentTransaction.show(fragmentList.get(1));
                fragmentTransaction.hide(fragmentList.get(0));
                fragmentTransaction.commit();
                back_state=1;
                break;
            case 2:
                Toast.makeText(MyApplication.getAppContext(),"就诊已结束"+pos,Toast.LENGTH_SHORT).show();
                break;
        }

    }
    public static void onItemLongClickDoing(int pos, int state){
        switch (state){
            case 0:
                Toast.makeText(MyApplication.getAppContext(),"未接诊，标为已读"+pos,Toast.LENGTH_SHORT).show();
                break;
            case 1:
                Toast.makeText(MyApplication.getAppContext(),"已接诊，标为已读"+pos,Toast.LENGTH_SHORT).show();
                break;
            case 2:
                Toast.makeText(MyApplication.getAppContext(),"就诊已结束"+pos,Toast.LENGTH_SHORT).show();
                break;
        }
    }
    public static void toMyConsultRoom02(){
        textView_top_name.setText("我的诊室");
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.show(fragmentList.get(1));
        fragmentTransaction.hide(fragmentList.get(0));
        fragmentTransaction.commit();
        back_state=1;

    }

    @Override
    public void openvideo(String strValue) {
        final View view;
        LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        view=((WebView)CordovaAppManager.mycordovaWebView.getView());
        addContentView(view,layoutParams);
    }

    private void callbackreceive(){
        myConsultRoomFragment02.setPatExitRoomCallBack(new MyConsultRoomFragment02.PatExitRoomCallBack() {
            @Override
            public void pat_exit_room(String strValue) {
                FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
                fragmentTransaction.show(fragmentList.get(0));
                fragmentTransaction.hide(fragmentList.get(1));
                fragmentTransaction.commit();
                back_state=0;
            }
        });
    }

    private void callback(){
    }



    //碎片1item的接诊状态(病人端退出诊室需要)
    public interface Fra01ItemStateBtn{
        public void setFra01ItemStateBtn();
    }

}
